package com.zhongzhi.thread;

import java.awt.List;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import com.zhongzhi.common.Common;
import com.zhongzhi.common.Plc;
import com.zhongzhi.common.Primary;
import com.zhongzhi.springboot.pojo.Equipment;
import com.zhongzhi.springboot.pojo.Station;
import com.zhongzhi.springboot.pojo.StationShow;
import com.zhongzhi.util.ClientUtil;
import com.zhongzhi.util.JsonResult;

import lombok.extern.log4j.Log4j;

@Log4j
public class TaskGuardTherad extends Thread{


	//工位信息集合
	ArrayList<StationShow> list =null;
	Set<Thread> keySet = null;				//线程对象
	Set<String> set=new HashSet<String>();	//线程名称

	/**
	 * 获取所有线程对象
	 * 获取线程对象的名字,组成一个集合
	 * 发起http请求,获取本机所管理工位集合
	 * 检查集合中是否有本机管理的工位线程名称,如果没有,则创建该工位线程
	 */
	@Override
	public void run() {
		// TODO Auto-generated method stub
		while (true) {
			//获取公共资源
			/**获取所有线程的对象*/
			keySet = Thread.getAllStackTraces().keySet();
			/**获取所有线程的名字*/

			for (Thread thread : keySet) {
				String name = thread.getName();
				set.add(name);
			}

			HashMap<Integer, Station> taskStationMap = Common.getTaskStationMap();
			Set<Integer> keySet2 = taskStationMap.keySet();
			for (Integer integer : keySet2) {
				Station station = taskStationMap.get(integer);
				String stationName = "task-"+station.getStationname()+ ": " +station.getId();
				boolean contains = set.contains(stationName);
				if (contains==false) {
					TaskTherad taskTherad = new TaskTherad();
					taskTherad.setName(stationName);
					taskTherad.setStationname(station.getStationname());
					taskTherad.start();
					taskTherad.setStationid(station.getId());
					Common.getTaskTherads().put(station.getId(), taskTherad);
					log.info("创建工位测试线程成功:"+stationName);
				}

			}
			
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}

		}

	}
}
